package com.itheima.web.controller.sms;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.service.sms.ICouponProductCategoryRelationService;
import com.itheima.service.sms.ICouponProductRelationService;
import com.itheima.service.sms.ICouponService;
import com.itheima.utils.PageResultList;
import com.itheima.utils.ResultData;
import com.itheima.vo.sms.Coupon;
import com.itheima.vo.sms.CouponProductCategoryRelation;
import com.itheima.vo.sms.CouponProductRelation;
import com.itheima.vo.sms.CouponVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优惠券表 前端控制器
 */
@RestController
@RequestMapping("/coupon")
@Slf4j
public class CouponController {
    @DubboReference
    private ICouponService couponService;
    @DubboReference
    private ICouponProductCategoryRelationService couponProductCategoryRelationService;
    @DubboReference
    private ICouponProductRelationService couponProductRelationService;

    /**
     * 添加优惠券方法
     * @param couponVo
     * @return
     */
    @PostMapping("/create")
    public ResultData create(@RequestBody CouponVo couponVo){
        log.info("couponVo:{}",couponVo);
        Coupon coupon=new Coupon();
        BeanUtils.copyProperties(couponVo,coupon);
        couponService.save(coupon);
        List<CouponProductCategoryRelation> productCategoryRelationList = couponVo.getProductCategoryRelationList();
       couponProductCategoryRelationService.saveBatch(productCategoryRelationList);
        List<CouponProductRelation> productRelationList = couponVo.getProductRelationList();
        couponProductRelationService.saveBatch(productRelationList);
        return ResultData.ok("添加成功","");
    }

    /**
     * 删除优惠券方法
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public ResultData remove(@PathVariable("id") Long id){
        couponService.removeById(id);
        couponService.delete(id);
        return ResultData.ok("删除成功","");
    }

    /**
     * 条件分页查询方法
     * @param name
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String name, Integer pageNum,
                           Integer pageSize, Integer type){
        Page<Coupon> list=couponService.getPage(name,type,pageNum,pageSize);
        PageResultList pageResult=new PageResultList();
        pageResult.setList(list.getRecords());
        pageResult.setPageSize(list.getSize());
        pageResult.setTotalPage(list.maxLimit());
        pageResult.setTotal(list.getTotal());
        pageResult.setPageNum(list.getPages());
        return ResultData.ok("查询成功",pageResult);
}

    /**
     * 更新优惠券方法
     * @param couponVo
     * @return
     */
    @PostMapping("/update/{id}")
public ResultData update(@RequestBody CouponVo couponVo){
    Coupon coupon=new Coupon();
    BeanUtils.copyProperties(couponVo,coupon);
    couponService.updateById(coupon);
    List<CouponProductRelation> productRelationList = couponVo.getProductRelationList();
    List<CouponProductCategoryRelation> productCategoryRelationList = couponVo.getProductCategoryRelationList();
    if (productCategoryRelationList!=null&&productCategoryRelationList.size()!=0){
        LambdaQueryWrapper<CouponProductCategoryRelation> lqw=new LambdaQueryWrapper<>();
        lqw.eq(CouponProductCategoryRelation::getCouponId,coupon.getId());
        couponProductCategoryRelationService.remove(lqw);
        couponProductCategoryRelationService.saveBatch(productCategoryRelationList);
    }
    if (productRelationList!=null&&productRelationList.size()!=0){
        LambdaQueryWrapper<CouponProductRelation> lqw=new LambdaQueryWrapper<>();
        lqw.eq(CouponProductRelation::getCouponId,coupon.getId());
        couponProductRelationService.remove(lqw);
        couponProductRelationService.saveBatch(productRelationList);
    }
    return ResultData.ok("修改成功","");

}

    /**
     * 根据id查询优惠券方法
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultData getById(@PathVariable("id") Long id){
        CouponVo couponVo= couponService.getOneMess(id);
        return ResultData.ok("",couponVo);
    }

}

